import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    wsConn: null,
    pagesAndFlvData: null,
    errorData: null,
    dwonLoadList: null,
    stopDownLoadCounter: 0,
    restoreDownLoadCounter: 0
  },
  mutations: {
    initWsConn(state, wsConn) {
      state.wsConn = wsConn;
    },
    setPagesAndFlvData(state, data) {
      state.pagesAndFlvData = data;
    },
    seterrorData(state, data) {
      state.errorData = data;
    },
    setDwonLoadList(state, data) {
      if (data === null) {
        data = state.dwonLoadList === null ? [] : null;
      }
      state.dwonLoadList = data;
    },
    updateProgress(state, data) {
      if (state.dwonLoadList != null) {
        state.dwonLoadList.forEach(e => {
          if (
            e.avid == data.avid &&
            e.cid == data.cid &&
            e.order == data.order
          ) {
            if (e.current <= data.current) {
              e.current = data.current;
            }
            return;
          }
        });
      }
    },
    stopDownLoadVideo(state, data) {
      state.stopDownLoadCounter += 1;
      if (state.dwonLoadList != null) {
        state.dwonLoadList.forEach(e => {
          if (
            e.avid == data.avid &&
            e.cid == data.cid &&
            e.order == data.order
          ) {
            // -1表示暂停
            e.state = -1;
            return;
          }
        });
      }
    },
    restoreDownLoadVideo(state, data) {
      if (state.dwonLoadList != null) {
        state.dwonLoadList.forEach(e => {
          data.urls.forEach(d => {
            if (
              e.avid == data.avid &&
              e.cid == data.cid &&
              e.order == d.order
            ) {
              // 1表示正在下载
              e.state = 1;
              return;
            }
          });
        });
      }
      state.restoreDownLoadCounter += 1;
    },
    stopAllDownLoadVideo(state) {
      if (state.dwonLoadList != null) {
        state.dwonLoadList.forEach(e => {
          if (e.state == 1) {
            e.state = -1;
          }
        });
      }
    },
    restoreAllDownLoadVideo(state) {
      if (state.dwonLoadList != null) {
        state.dwonLoadList.forEach(e => {
          if (e.state == -1) {
            e.state = 1;
          }
        });
      }
      state.restoreDownLoadCounter += 1;
    }
  },
  actions: {},
  modules: {}
});
